const admin_url = window.itboye.admin_url;
const api_url = window.itboye.api_url;

const getAdminUrl = url => {
    //除去(Admin or admin)
    url = url.replace(/(A|a)dmin\//, '');

    //替换控制器驼峰为下划线
    let func = url.substr(url.indexOf('/'));
    let controller = url.substr(0, url.indexOf('/')).replace(/([a-zA-Z])([A-Z])/g, '$1_$2').toLowerCase();

    return `${admin_url}/Admin/${controller}${func}`;
};

const getAvatarUrl = (id, size = 120) => {
    return `${api_url}/picture/avatar?uid=${id}&size=${size}`;
};

const getKeyInObject = (object, exp) => {
    if (typeof object == 'undefined') return undefined;
    let args = exp.split('.');
    let o = object;
    for (let val of args) {
        if (typeof o === 'undefined' || !o.hasOwnProperty(val)) return undefined;
        o = o[val];
    }
    return o;
};
const returnTop = () => {
    window.scrollBy(0, -100);
    if (document.body.scrollTop > 0) {
        setTimeout(returnTop, 50);
    }
};

const tools = {
    getAdminUrl, getAvatarUrl, getKeyInObject, returnTop
};

export default tools;